package com.oromwin.repos.common;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.server.request.Request;
import com.oromwin.repos.server.request.file.CopyFileRequest;
import com.oromwin.repos.server.request.file.CreateFileRequest;
import com.oromwin.repos.server.request.file.DeleteFileRequest;
import com.oromwin.repos.server.request.file.InfoFileRequest;
import com.oromwin.repos.server.request.file.MoveFileRequest;
import com.oromwin.repos.server.request.file.ReadFileRequest;
import com.oromwin.repos.server.request.file.RenameFileRequest;
import com.oromwin.repos.server.request.volume.CreateVolumeRequest;

public class Server {
    private static Log log = Logger.getLog(Server.class);
    private static long RESERVED = ReposConfig.getInstance().getReposLocalDiskUsageReserved();

    public enum LockType { READ, WRITE }

    public enum ReposType { FILE, MEDIA, VOLUME }

    public static class UniqueId {
        public static String generate() {
            return UUID.randomUUID().toString();
        }
    }

    public static LockType getLockTypeOfPacket(Request req) {
        if (req instanceof CreateFileRequest) {
            return LockType.WRITE;
        } else if (req instanceof CreateVolumeRequest) {
            return LockType.WRITE;
        } else {
            return LockType.READ;
        }
    }

    public static Long freeSpace(File file) throws ReposException {
        try {
            Long freeSpace = new Long(file.getUsableSpace() - RESERVED);
            return freeSpace;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, "Input path is null, {"+file+"}");
        } catch (SecurityException e) {
            throw ReposException.create(log, e, "Can't access to path, {"+file+"}");
        }
    }

    public static File mktempfile(String filename) throws NullPointerException, SecurityException, IOException {
        String suffix = Common.Separator.PERIOD + Common.FileExt.TEMPFILE;
        File file = File.createTempFile(filename, suffix);
        file.createNewFile();
        return file;
    }

    /**
     * Copy, Delete, Read, Move, Rename request only need to lock for requested FileId 
     * @param dis
     * @param req
     * @return
     * @throws IOException
     */
    public static String identifyUniqueId(DataInputStream dis, Request req) throws IOException {
        String FileId = null;
        if (req instanceof CopyFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else if (req instanceof DeleteFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else if (req instanceof InfoFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else if (req instanceof ReadFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else if (req instanceof MoveFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else if (req instanceof RenameFileRequest) {
            FileId = IOHelper.readUTF8(dis);
        } else {
            FileId = req.id();
        }
        return FileId;
    }

}
